#include "manageform.h"
#include "ui_manageform.h"

#include "animeinfodialog.h"
#include "flowcontroler.h"
#include "postdetaildialog.h"

#include <QMessageBox>



ManageForm::ManageForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ManageForm)
{
    ui->setupUi(this);

    connect(FlowControler::Interface, &XInterface::getAllAnimeFinished, this,  [=](int ret, QList<Core::Anime> animes)
    {
        if(ret == NoError)
        {
            updateAnimes(animes);
        }
    });
    connect(FlowControler::Interface, &XInterface::getPendingPostFinished, this,  [=](int ret, QList<Core::Post> posts)
    {
        if(ret == NoError)
        {
            updatependingPosts(posts);
        }
    });
    connect(FlowControler::Interface, &XInterface::getPendingCommentFinished, this,  [=](int ret, QList<Core::Comment> comments)
    {
        if(ret == NoError)
        {
            updatependingComments(comments);
        }
    });

    connect(FlowControler::Interface, &XInterface::addAnimeFinished, this,  &ManageForm::animeChanged);
    connect(FlowControler::Interface, &XInterface::deleteAnimeFinished, this,  &ManageForm::animeChanged);
    connect(FlowControler::Interface, &XInterface::updateAnimeFinished, this,  &ManageForm::animeChanged);


    m_model = new QStandardItemModel();
    ui->tableView->setModel(m_model);

    QStringList list{
        "标题", "放送日期", "制作团队", "声优", "观看链接", "标签", "简介"
    };
    m_model->setHorizontalHeaderLabels(list);

    ui->tableView->setSelectionBehavior(QTableView::SelectionBehavior::SelectRows);
    ui->tableView->setSelectionMode(QTableView::SingleSelection);
    ui->tableView->setEditTriggers(QTableView::NoEditTriggers);

    ui->tableView->horizontalHeader()->setSectionResizeMode(4, QHeaderView::Fixed);
    ui->tableView->setColumnWidth(4, 150);
    ui->tableView->horizontalHeader()->setStretchLastSection(true);

    m_postsModel = new QStandardItemModel();
    ui->tableView_3->setModel(m_postsModel);

    QStringList list2{ "用户", "时间", "主题" ,  "详细内容"
    };
    m_postsModel->setHorizontalHeaderLabels(list2);

    ui->tableView_3->setSelectionBehavior(QTableView::SelectionBehavior::SelectRows);
    ui->tableView_3->setSelectionMode(QTableView::SingleSelection);
    ui->tableView_3->setEditTriggers(QTableView::NoEditTriggers);

    ui->tableView_3->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed);
    ui->tableView_3->setColumnWidth(1, 150);
    ui->tableView_3->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Fixed);
    ui->tableView_3->setColumnWidth(2, 180);
    ui->tableView_3->horizontalHeader()->setStretchLastSection(true);


    m_commentsModel = new QStandardItemModel();
    ui->tableView_2->setModel(m_commentsModel);

    QStringList list3{ "用户", "评论动漫" , "时间", "评论内容"
    };
    m_commentsModel->setHorizontalHeaderLabels(list3);

    ui->tableView_2->setSelectionBehavior(QTableView::SelectionBehavior::SelectRows);
    ui->tableView_2->setSelectionMode(QTableView::SingleSelection);
    ui->tableView_2->setEditTriggers(QTableView::NoEditTriggers);

//    ui->tableView_2->horizontalHeader()->setSectionResizeMode(4, QHeaderView::Fixed);
//    ui->tableView_2->setColumnWidth(4, 150);
    ui->tableView_2->horizontalHeader()->setStretchLastSection(true);
//    FlowControler::Interface->getAllAnimes();

    ui->tabWidget->setCurrentIndex(0);
}

ManageForm::~ManageForm()
{
    delete ui;
}

void ManageForm::updateAnimes(QList<Core::Anime> animes)
{

    m_animes = animes;

    m_model->setRowCount(0);
    m_model->setRowCount(animes.size());


    for(int i=0; i < animes.size(); i++)
    {
        Core::Anime &anime = animes[i];
        m_model->setData(m_model->index(i, 0), anime.title, Qt::DisplayRole);
        m_model->setData(m_model->index(i, 1), anime.releaseDate, Qt::DisplayRole);
        m_model->setData(m_model->index(i, 2), anime.staff, Qt::DisplayRole);
        m_model->setData(m_model->index(i, 3), anime.voiceCast.join(","), Qt::DisplayRole);
        m_model->setData(m_model->index(i, 4), anime.watchLink, Qt::DisplayRole);
        m_model->setData(m_model->index(i, 5), anime.tags.join(","), Qt::DisplayRole);
        m_model->setData(m_model->index(i, 6), anime.synopsis, Qt::DisplayRole);
    }
}

void ManageForm::updatependingPosts(QList<Core::Post> posts)
{
    m_pendingPosts = posts;

    m_postsModel->setRowCount(0);
    m_postsModel->setRowCount(posts.size());


    for(int i=0; i < posts.size(); i++)
    {
        Core::Post &post = posts[i];
        m_postsModel->setData(m_postsModel->index(i, 0), post.userName, Qt::DisplayRole);
        m_postsModel->setData(m_postsModel->index(i, 1), post.createdAt, Qt::DisplayRole);
        m_postsModel->setData(m_postsModel->index(i, 2), post.title, Qt::DisplayRole);
        m_postsModel->setData(m_postsModel->index(i, 3), post.content, Qt::DisplayRole);
    }
}

void ManageForm::updatependingComments(QList<Core::Comment> comments)
{
    m_pendingComments = comments;

    m_commentsModel->setRowCount(0);
    m_commentsModel->setRowCount(comments.size());


    for(int i=0; i < comments.size(); i++)
    {
        Core::Comment &comment = comments[i];
        QString name = findAnimeById(comment.animeId);
        m_commentsModel->setData(m_commentsModel->index(i, 0), comment.userName, Qt::DisplayRole);
        m_commentsModel->setData(m_commentsModel->index(i, 1), name, Qt::DisplayRole);
        m_commentsModel->setData(m_commentsModel->index(i, 2), comment.createdAt, Qt::DisplayRole);
        m_commentsModel->setData(m_commentsModel->index(i, 3), comment.content, Qt::DisplayRole);
    }
}

void ManageForm::animeChanged(int ret)
{
    if(ret == NoError)
    {
//        FlowControler::Interface->getAllAnimes();
    }
}

void ManageForm::on_btnAdd_clicked()
{
    AnimeInfoDialog dialog;
    dialog.exec();
}

void ManageForm::on_btnDelete_clicked()
{
    int row = ui->tableView->currentIndex().row();
    if(row < 0)
    {
        QMessageBox::warning(this, "警告", "请选择动漫信息");
        return;
    }

    Core::Anime anime = m_animes[row];
    FlowControler::Interface->deleteAnime(anime);
}

void ManageForm::on_btnEdit_clicked()
{
    int row = ui->tableView->currentIndex().row();
    if(row < 0)
    {
        QMessageBox::warning(this, "警告", "请选择动漫信息");
        return;
    }

    Core::Anime anime = m_animes[row];

    AnimeInfoDialog dialog;
    dialog.setAnime(anime);
    dialog.exec();
}

void ManageForm::on_btnAccept_clicked()
{
    int row = ui->tableView_2->currentIndex().row();
    if(row < 0)
    {
        QMessageBox::warning(this, "警告", "请选择评论信息");
        return;
    }

    FlowControler::Interface->acceptComment(m_pendingComments[row]);
}

void ManageForm::on_btnDeleteComment_clicked()
{
    int row = ui->tableView_2->currentIndex().row();
    if(row < 0)
    {
        QMessageBox::warning(this, "警告", "请选择评论信息");
        return;
    }

    FlowControler::Interface->rejectComment(m_pendingComments[row]);
}

QString ManageForm::findAnimeById(int id)
{
    for(auto p : m_animes)
    {
        if(p.id == id)
        {
            return p.title;
        }
    }

    return "";
}

void ManageForm::on_btnAccept_2_clicked()
{
    int row = ui->tableView_3->currentIndex().row();
    if(row < 0)
    {
        QMessageBox::warning(this, "警告", "请选择帖子");
        return;
    }

    FlowControler::Interface->acceptpost(m_pendingPosts[row]);
}

void ManageForm::on_btnDeletePost_clicked()
{
    int row = ui->tableView_3->currentIndex().row();
    if(row < 0)
    {
        QMessageBox::warning(this, "警告", "请选择帖子");
        return;
    }

    FlowControler::Interface->rejectpost(m_pendingPosts[row]);
}

void ManageForm::on_btnViewPost_clicked()
{
    int row = ui->tableView_3->currentIndex().row();
    if(row < 0)
    {
        QMessageBox::warning(this, "警告", "请选择帖子");
        return;
    }

    PostDetailDialog dialog;
    dialog.setPost(m_pendingPosts[row]);
    dialog.exec();
}
